/**
 * Created by weng on 2017/8/16.
 */

var data_temp = {};
var  temp_page_size= 15;

/**
 * 获取推送历史记录
 * @param info
 */
var getPushInfo = function () {
    console.log("获取信息");
    var obj = {};
    obj.startTime = $("#startTime").val();
    obj.endTime =  moment($("#endTime").val()).format('YYYY-MM-DD 23:59:59');
    console.log("obj",obj);

    $.ajax({
        url:'/hospital/getPushInfo',
        type:'post',
        dataType:'json',
        data:obj,
        success:function(data) {
            console.log("data",data);
            if (data.code != 200) {
                alert("查询错误");
                return;
            } else {
                if(data.info.length > 0){
                    data_temp = data.info
                    var temp_teble_info = data_temp.slice(0 , temp_page_size);
                    createPageMin(temp_page_size, 5, Math.ceil(data_temp.length), data_temp);
                    showTableInfo(temp_teble_info,"specialOrderList");
                }
            }
        }
    });
};



function createPageMin(pageSize, buttons, total,data_temp) {
    $("#pagination").empty();
    $('#pagination').Paging({pagesize:pageSize,count:total,toolbar:true,pageSizeList:[temp_page_size],
        callback:function(page,size,count){
            page = page - 1;
            var temp_teble_info = data_temp.slice(page  * size ,page * pageSize + size);
            showTableInfo(temp_teble_info,"specialOrderList");
        }});
}


function showTableInfo(data_arr,id){
    var html = "";
    var thead = $('#'+id + ' thead');
    var tbody = $('#'+id + ' tbody');

    tbody.empty();
    thead.empty();
    var htmlHead = "<tr>"
        + "<th>推送时间：</th>"
        + "<th>标题：</th>"
        + "<th>内容：</th>"
        + "<th>电话：</th>"
        + "<th>图片地址：</th>"
        + "<th>其他详情：</th>"
        + "</tr>"
    htmlHead = $(htmlHead);
    thead.append(htmlHead);

    for(var i in data_arr){
        var time_des = data_arr[i].time;
        html += '<tr>' +
        '<td style="display: none" id="id'+ data_arr[i].id +'">'+  data_arr[i].id + '</td>'
        + '<td  id="title'+ data_arr[i].id +'">'+  moment(data_arr[i].time).format('YYYY-MM-DD hh:mm:ss') + '</td>'
        + '<td  id="title'+ data_arr[i].id +'">'+  data_arr[i].title + '</td>'
        +  '<td  id="msg:'+ data_arr[i].id +'">'+  data_arr[i].msg + '</td>'
        +  '<td  id="msg:'+ data_arr[i].id +'">'+  data_arr[i].phone + '</td>'
        +   '<td  id="url:'+ data_arr[i].id +'"><img style="height:100px" src='+  data_arr[i].url + ' /></td>'
        +   '<th><a onclick="shoPushDetail('+ data_arr[i].id +')">健康信息内容</a></th>';
            html += '</th>';
            html += '</tr>';
    }
    var tr = $(html);
    tbody.append(tr);
}


function shoPushDetail(id) {
    console.log("id is",id)
    var url="/hospital/shoPushDetail";
    $.ajax({
        url:url,
        type: 'POST',
        cache: false,
        async:false,
        data: {id:id},
        success:function(data){
            console.log(data)
            if(200 === data.code) {
                var data_arr = data.info;
                var htmlHead = "<tr>"
                    + "<th>描述：</th>"
                    + "<th>值：</th>"
                    + "</tr>";

                var html = "";
                for(var i in data_arr){
                    html += '<tr>'
                        +  '<td  id="key:'+ data_arr[i].id +'">'+  data_arr[i].key + '</td>'
                        +   '<td  id="value:'+ data_arr[i].id +'">'+  data_arr[i].value + '</td>'
                    html += '</th>';
                    html += '</tr>';
                }
                console.log(htmlHead)
                console.log(html)

                layer.open({
                    type: 1,
                    skin: 'layui-layer-rim', //加上边框
                    title:"推送的健康信息", //样式类名
                   // closeBtn: 0, //不显示关闭按钮
                    area: ['500px', '400px'],
                   // shadeClose: true, //开启遮罩关闭
                    content: "<table class='table table-bordered'>"
                    + "<thead>"+htmlHead+"</thead>"
                    + "<tbody>"+html+"</tbody>"
                    + "</table>"
                });
            }else {
                layer.msg(data.info );
            }
        }
    });
}

function addPushInfo(){
    layer.prompt(
        {title: '输入描述信息',
           // value:'eg:2015-06-01',
            formType: 0
        },
        function(key, indexTime){
            layer.close(indexTime);
            layer.prompt({
                formType:0,
             //   value:'eg:9:00~10:00 多个时间段用 ， 隔开',
                title:'输入值',
            },function(value,index){
                var html = "";
                var tbody = $('#info tbody');
                html += '<tr>' +
                    '<td  ">'+ key + '</th>' +
                    '<td  ">'+ value + '</th>' ;
                html += '</tr>';
                var tr = $(html);
                tbody.append(tr);
                layer.close(index);
            });
        });
}


/**
 * 推送信息
 * @constructor
 */
function PushInfo(){
     layer.msg('你确定推送这些信息么？', {
         time: 0 //不自动关闭
         ,btn: ['确定', '取消']
         ,yes: function(index){
            var obj = {};
            obj.phone = $("#phone").val()
            obj.title = $("#title").val();
            obj.msg =$("#msg").val();

            if(!obj.title){
                return   layer.msg("请填写标题");
            }else if(!obj.msg){
                return   layer.msg("请填写内容");
            }

            console.log("formData",formData)
            var info = [];
            $("#info tbody").find("tr").each(function () {
                //第二列单元格的值eq(索引)
                var key = $(this).children('td:eq(0)').text();
                var value = $(this).children('td:eq(1)').text();
                info.push({key:key,value:value});
            });
            obj.info = JSON.stringify(info);

            var formData = new FormData();
            formData.append('files',$("#file_up")[0].files[0]);
            formData.append('id',obj.id);
            formData.append('title',obj.title);
            formData.append('msg',obj.msg );
            formData.append('info',obj.info );
            formData.append('phone',obj.phone );
            var url="/hospital/pushJG";
            $.ajax({
                url:url,
                type: 'POST',
                cache: false,
                data: formData,
                processData: false,
                contentType: false,
                success:function(data){
                    layer.close(index);
                    if(200 === data.code) {
                        layer.msg('推送成功', {
                            icon: 1,
                            time: 1000 //2秒关闭（如果不配置，默认是3秒）
                        }, function(){
                            location.reload();
                        });
                    }else {
                        layer.msg(data.info );
                    }
                }
            });
         }
     });
}

var changeBusTime = function (id) {
    layer.prompt({
        formType:0,
        value:'eg:9:00~10:00 多个时间段用 ， 隔开',
        title:'修改班次时间',
    },function(value,index){
        var obj = {};
        obj.bustype = value;
        obj.id = id;
        var url="/hospital/changeBusTime";
        $.ajax({
            type:'POST',
            url:url,
            data:obj,
            async:false,
            success:function(data){
                layer.close(index);
                if (data.code != 200) {
                    layer.msg("修改失败" );
                } else {
                    layer.msg('修改成功', {
                        //icon: 1,
                        time: 1000 //2秒关闭（如果不配置，默认是3秒）
                    }, function(){
                        location.reload();
                    });
                }
            }
        });

    });
}




var addBussTime = function (id) {
    layer.prompt(
        {title: '输入时间',
            value:'eg:2015-06-01',
            formType: 0
        },
        function(valueTime, indexTime){
        layer.close(indexTime);

        layer.prompt({
            formType:0,
            value:'eg:9:00~10:00 多个时间段用 ， 隔开',
            title:'修改班次时间',
        },function(value,index){
            var obj = {};
            obj.time = valueTime;
            obj.bustype = value;
            obj.id = id;
            var url="/hospital/addBussTime";
            $.ajax({
                type:'POST',
                url:url,
                data:obj,
                async:false,
                success:function(data){
                    layer.close(index);
                    if (data.code != 200) {
                        layer.msg("修改失败" );
                    } else {
                        layer.msg('修改成功', {
                            //icon: 1,
                            time: 1000 //2秒关闭（如果不配置，默认是3秒）
                        }, function(){
                            location.reload();
                        });
                    }
                }
            });

        });
    });
}

var delBusTime = function (id) {
    layer.msg('你确定删除么？', {
        time: 0 //不自动关闭
        ,btn: ['确定', '取消']
        ,yes: function(index){
            var obj = {};
            obj.id = id;
            var url="/hospital/delBusTime";
            $.ajax({
                url:url,
                type:'post',
                dataType:'json',
                data:obj,
                success:function(data){
                    layer.close(index);
                    if(200 === data.code) {
                        layer.msg('刪除成功', {
                            icon: 1,
                            time: 1000 //2秒关闭（如果不配置，默认是3秒）
                        }, function(){
                            location.reload();
                        });
                    }else {
                        layer.msg(data.info );
                    }
                }
            });
        }
    });
}